#include "game.h"
#include "drawer.h"
#include "scene.h"

#include <iostream>
#include <SDL/SDL_gfxPrimitives.h>
#include <string>
#include <sstream>
#include "input.h"

using namespace std;

class CCreep;

SDL_Surface * screen;

extern CInput OInput;

CDrawer::CDrawer(CScene *scene) {
    gamescene = scene;
}

CDrawer::~CDrawer() {

}

void CDrawer::drawAll() {
    for (auto it = ++level1.begin(); it != level1.end(); it++) {
        auto it2 = --it;
        it++;
        int x1 = (*it).x;
        int x2 = (*it2).x;
        int y1 = (*it).y;
        int y2 = (*it2).y;
        lineRGBA(screen,x1,y1,x2,y2,0xFF,0xFF,0xFF,0xFF);
    }
    auto it = --level1.end();
    int x1 = (*it).x;
    int y1 = (*it).y;
    int x2 = level1ciel.x;
    int y2 = level1ciel.y;
    lineRGBA(screen,x1,y1,x2,y2,0xFF,0xFF,0xFF,0xFF);
    for (auto it = gamescene->obj()->begin(); it != gamescene->obj()->end(); it++) {
        (*it)->Draw();
    }
    lineRGBA(screen,800,0,800,600,0xFF,0xFF,0xFF,0xFF);
    rectangleRGBA(screen,800,150,1024,200,0xFF,0xFF,0xFF,0xFF);


    temp = RenderText(font,"Pocet Creepov:");
    BlitSurface(800,0,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,gamescene->numCreeps());
    BlitSurface(900,0,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,"Pocet Utecencov:");
    BlitSurface(800,15,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,gamescene->getEscaped());
    BlitSurface(900,15,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,"Vlna");
    BlitSurface(800,30,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,gamescene->GetWave());
    BlitSurface(840,30,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,"Credits:");
    BlitSurface(800,45,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,gamescene->GetCredits());
    BlitSurface(900,45,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,"Mouse State:");
    BlitSurface(800,100,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,OInput.GetState());
    BlitSurface(900,100,temp,screen);

    SDL_FreeSurface(temp);
    temp = RenderText(font,"Build");
    BlitSurface(900,170,temp,screen);

    if (OInput.GetState() == PSTATE_SELECTED) {

        SDL_FreeSurface(temp);
        temp = RenderText(font,"Tower Range:");
        BlitSurface(800,215,temp,screen);

        SDL_FreeSurface(temp);
        temp = RenderText(font,OInput.GetSelected()->GetRadius());
        BlitSurface(900,215,temp,screen);

        SDL_FreeSurface(temp);
        temp = RenderText(font,"Tower Damage:");
        BlitSurface(800,230,temp,screen);

        SDL_FreeSurface(temp);
        temp = RenderText(font,OInput.GetSelected()->GetDMG());
        BlitSurface(900,230,temp,screen);

        SDL_FreeSurface(temp);
        temp = RenderText(font,"Shots per second:");
        BlitSurface(800,245,temp,screen);

        SDL_FreeSurface(temp);
        temp = RenderText(font,OInput.GetSelected()->GetSpeedRatio());
        BlitSurface(900,245,temp,screen);
    }

    SDL_FreeSurface(temp);
    temp = RenderText(font,"Creep stats:");
    BlitSurface(800,400,temp,screen);
    SDL_FreeSurface(temp);
    temp = RenderText(font,"------------");
    BlitSurface(800,415,temp,screen);
    SDL_FreeSurface(temp);
    temp = RenderText(font,"Moves per second:");
    BlitSurface(800,430,temp,screen);
    SDL_FreeSurface(temp);
    temp = RenderText(font,1);
    BlitSurface(910,430,temp,screen);
    SDL_FreeSurface(temp);
    temp = RenderText(font,"Total hitpoints:");
    BlitSurface(800,445,temp,screen);
    SDL_FreeSurface(temp);
    temp = RenderText(font,gamescene->GetWave());
    BlitSurface(900,445,temp,screen);
    SDL_FreeSurface(temp);

    SDL_Flip(screen);
}

void CDrawer::initDrawer() {
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode(1024,600,32,SDL_SWSURFACE);
    TTF_Init();
    SDL_WM_SetCaption("GeometryTD Alpha v0.2",NULL);
    font = TTF_OpenFont("arial.ttf",12);
    if (font == NULL) {
        cout << "[CORE] Error loading font!" << endl;
    }

    stringstream ss;
    ss << gamescene->numCreeps();    

    return;
}
